home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 July
/
Macworld (1999-07).dmg
/
Shareware World
/
Info
/
For Developers
/
Mops 3.4.sea
/
Quick Edit ƒ
/
Subject Glossary
/
Control Structures
< prev
next >
Wrap
Text File
|
1996-01-24
|
6KB
|
201 lines
IF/THEN
The stack notation for a conditional phrase flag is ?. ? need not be a boolean
as any non-zero number will be "true" for branching purposes (not to be confused
with the boolean true which can only be -1).
: xxx ( ? -- ) \ execute yyy if ? is non-zero
IF yyy
THEN ;
: xxx ( ? -- ) \ execute yyy if ? is non-zero, else execute zzz
IF yyy
ELSE zzz
THEN ;
: xxx ( ? -- ) \ execute yyy if ? is zero
NIF yyy
THEN ;
: xxx ( ? -- ) \ execute yyy if ? is zero, else execute zzz
NIF yyy
ELSE zzz
THEN ;
Note that NIF will compile identically to 0= IF
?exit ( ? -- ) equivalent to IF exit THEN
0exit ( ? -- ) equivalent to 0= IF exit THEN
CASE/SELECT
CASE -- Begins a CASE .. OF .. ENDOF ... ENDCASE structure.
OF n1 n2 -- | n1
Use within a CASE structure. Yields n1 and jumps to beyond next ENDOF
if no match.
ENDOF -- Use in CASE structure.
RANGEOF val lo hi --
Use in place of an OF in a CASE structure. Provides conditional
execution if val is within range lo to hi inclusive.
ENDCASE n -- Marks end of CASE structure.
: xxx ( n -- )
CASE
n1 OF ... ENDOF
n2 OF ... ENDOF
n3 OF ... ENDOF
lo hi RANGEOF ... ENDOF \ do this if lo ≤ n ≤ hi
... \ default procedures
ENDCASE ;
IS{ 240 index -- index here 240 Used in a select{ construct.
SELECT{ --
defines a positional case construct - see Forth Dimensions vII p.51. It
is smaller and faster than the equivalent CASE construct, as long as
there are more than a couple of values. Values must be >= 0, and we
give a warning if a value > 50 is used, which could well be a boo-boo.
DEFAULT{ -- Used in a select{ construct.
}END -- Used in a select{ construct.
}SELECT -- Used in a select{ construct.
type{ mytype0 mytype1 mytype2 ... } Used to define a contiguous set of
constants, starting with zero. Convenient for use in SELECT{.
: xxx ( n -- )
SELECT{
mytype0 IS ... }END
mytype1 IS ... }END
mytype2 IS ... }END
DEFAULT{ ...
}SELECT ;
The advantage of SELECT{ over CASE is that it is faster.
CASE[ is another keyed CASE. Each test value or range is compiled into a
pair of 2-byte entries in a table. Compilation is turned off and on while
getting the test values, which are evaluated at compile time. This is slightly
less flexible than Eaker's CASE, but is faster and more compact. It is also
adequate for the majority of keyed case needs. When you want a positional
case, SELECT{ is still the best. See file View for an example use.
CASE[
]=>
],
RANGE]=>
RANGE],
DEFAULT=>
]CASE
example:
: qq db
case[ 21 ]=> 210
[ 22 ]=> 220
[ 80 ], [ 82 ], [ 84 ], [ 86 ]=> 888
[ 30 40 range]=> 333
[ 90 ], [ 92 ], [ 170 ]=> -999
[ 90 ], [ 92 ], [ 100 150 range], [ 170 ]=> -999
[ 222 ]=> 2220
default=> 99
]case ;
: q db
select[ 3 ]=> 23
[ 2 ]=> 22
\ [ 0 ]=> 20
[ 8 ]=> 28
default=> 999
]select ;
DO/LOOP
BOUNDS addr cnt -- limit addr
Equivalent to OVER + SWAP. Useful for setting up many DO loops.
DO end beg --
Marks beginning of DO ... LOOP sequence. Will always execute at least
once, even if end ≤ beg. See ?DO. DO loops are slightly different,
although the change won't affect most existing code. The loop is
considered to be finished if the index crosses the boundary between the
limit and the limit minus one, in either direction. The main effect is
to make loops go around one more time if the index is being counted
down.
?DO end beg --
Marks beginning of ?DO ... LOOP sequence. Will not execute even once
if end ≤ beg. See DO.
LOOP -- Marks end of DO ... LOOP structure.
+LOOP n --
Marks end of DO ...+LOOP structure. Like LOOP except will increment i
by n.
I -- n
We keep the loop index I in D3, but the return stack is entirely in
memory so that words can be called simply with BSR/JSR. This means that
I can be used in words called from within DO loops. In fact I can be
used as another local variable.
J -- n Index value for the next outer DO ... LOOP control structure.
K -- n Index value for the 2nd outer DO ... LOOP control structure.
UNLOOP --
ANSI standard. Use it if you want to EXIT from within a DO loop - put
UNLOOP before the EXIT. It removes the loop stuff from the return
stack.
LEAVE --
There is also a change to LEAVE, which is now a "leaping LEAVE". When
LEAVE is executed, it now causes the loop to be left immediately, rather
than waiting until the next time LOOP is encountered.
: xxx
( end beg ) DO ... LOOP ; \ i will increment by 1
: xxx
( end beg ) DO ... ( incr) +LOOP ; \ i will increment by incr
: xxx
( end beg ) DO ...
( ? ) IF LEAVE THEN
...
LOOP ;
FOR limit --
Marks beginning of a FOR ... NEXT loop. These are simple loops that
have less overhead than DO loops. The loop index i counts down from
limit-1 to zero. The initial limit must be less than 64K. Uses a DBRA
loop instruction for speed. You can't LEAVE a FOR ... NEXT loop.
NEXT -- Marks end of a FOR ... NEXT loop. See FOR.
: xxx ( limit ) FOR ... NEXT ; \ i is available
example:
: go
4 FOR i . NEXT ;
go
3 2 1 0 0->
BEGIN/AGAIN
BEGIN -- standard. Marks the beginning of a BEGIN ... (WHILE/NWHILE/UNTIL/NUNTIL/AGAIN) looping structure.
AGAIN -- Standard. Marks bounds of BEGIN ... AGAIN loop.
NUNTIL ? -- Similar to until. Stop if ? is-zero.
NWHILE ? -- Similar to while. Continue if ? is zero.
REPEAT -- Standard. Marks bounds of BEGIN ... XXX ... REPEAT loop.
UNTIL ? -- standard. Stop if ? is non-zero.
WHILE ? -- standard. Continue if ? is non-zero.
OTHER
EXIT --
Terminates execution of the current word or method, and returns control
to the next higher word on the return stack.